#! /usr/bin/env bash
EXTRA_ARGS=$@

# Text color variables
if [[ $TERM != *"xterm"* ]]; then
    export TERM=dumb
fi
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
#txtblu=$(tput setaf 4)    # Blue
#txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
#txtwht=$(tput setaf 7)    # White
txtrst=$(tput sgr0)       # Text reset

function check_valid()
{
    if [ "${!1}" == "YES" ] || [ "${!1}" == "ON" ]; then
	echo "YES"
	return 1
    fi
    if [ "${!1}" == "NO" ] || [ "${!1}" == "OFF" ]; then
	echo "NO"
	return 1
    fi
    echo "Invalid value for $1 (${!1}) -- Must be ON, YES, NO, or OFF"
    exit 1
}

### Set to YES for parallel compile; otherwise NO for serial (default)
MPI="${MPI:-NO}"
MPI=$(check_valid MPI)
echo "${txtgrn}MPI set to ${MPI}${txtrst}"

### Switch for Debug or Release build:
BUILD_TYPE="${BUILD_TYPE:-Release}"
#BUILD_TYPE=Debug

SHARED="${SHARED:-YES}"
SHARED=$(check_valid SHARED)

### Define to YES to *disable* all exodus deprecated functions
OMIT_DEPRECATED_CODE="NO"

### The following assumes you are building in a subdirectory of ACCESS Root
### If not, then define "ACCESS" to point to the root of the SEACAS source code.
ACCESS=$(cd ..; pwd)

### If you do not have the X11 developer package on your system
### which provides X11/Xlib.h and the libX11, then change the "YES"
### below to "NO". It will disable blot and fastq
HAVE_X11="${HAVE_X11:-YES}"
HAVE_X11=$(check_valid HAVE_X11)

### Python Version...
PYTHON_VER=${PYTHON_VER:-"3.0"}

### Set to YES to enable the building of a thread-safe version of the Exodus and IOSS libraries.
THREADSAFE=${THREADSAFE:-NO}
THREADSAFE=$(check_valid THREADSAFE)

### The SEACAS code will install in ${INSTALL_PATH}/bin, ${INSTALL_PATH}/lib, and ${INSTALL_PATH}/include.
INSTALL_PATH=${ACCESS}

### TPLs --
### Using SEMS (Software Engineering Maintenance & Support) provided libraries...
### https://sems.sandia.gov/
module purge
source /projects/sems/modulefiles/utils/sems-modules-init.sh
module load sems-gcc/13.2.0
module load sems-fmt/10.2.1
module load sems-cmake/3.24.2
module load sems-doxygen
module load sems-zlib
+module load sems-python/3.9.0

if [ "$MPI" == "YES" ]
then
  module load sems-openmpi
  module load sems-hdf5/1.10.7
  module load sems-netcdf-c/4.7.3
  module load sems-parallel-netcdf/1.12.3
  module load sems-parmetis/4.0.3
  module load sems-cgns/4.4.0
else
  module load sems-hdf5-serial/1.14.3
  module load sems-netcdf-c-serial/4.9.2
  module load sems-cgns-serial/4.4.0
  module load sems-metis/5.1.0 
fi

HAVE_NETCDF=YES
HAVE_CGNS=YES

# NOTE: MATIO, is currently not supported by SEMS.
#       To use it you will need to install them following the directions in
#       README.md, enable them below, and add the appropriate defines below:
#
# -D Matio_LIBRARY_DIRS:PATH=${MATIO_PATH}/lib \
# -D TPL_Matio_INCLUDE_DIRS:PATH=${MATIO_PATH}/include \

HAVE_MATIO=NO

NETCDF_PATH=${SEMS_NETCDF_ROOT}
HDF5_PATH=${SEMS_HDF5_ROOT}
CGNS_PATH=${SEMS_CGNS_ROOT}

### Define to NO to *enable* exodus deprecated functions
OMIT_DEPRECATED_CODE="NO"

### Set to YES to use Kokkos in the Ioss library; otherwise NO (default)
KOKKOS=NO

### Set to YES for CUDA compile; otherwise NO (default) (only used if KOKKOS=YES
CUDA=NO

### Change this to point to the compilers you want to use
COMPILER="gnu"
#COMPILER="clang"
if [ "$MPI" == "YES" ]
then
  MPI_EXEC=`which mpiexec`
  MPI_BIN=`dirname $MPI_EXEC`

  CXX=mpicxx
  CC=mpicc
  FC=mpif77

else
  CXX=g++
  CC=gcc
  FC=gfortran
fi

if [ "$KOKKOS" == "YES" ]
then
  if [ "$CUDA" == "YES" ]
  then
    export "OMPI_CXX=${SEACAS_SRC_DIR}/packages/kokkos/config/nvcc_wrapper"
    export CUDA_MANAGED_FORCE_DEVICE_ALLOC=1
    KOKKOS_SYMBOLS="-D Seacas_ENABLE_Kokkos:BOOL=ON \
                    -D TPL_ENABLE_CUDA:Bool=ON \
                    -D CUDA_TOOLKIT_ROOT_DIR:PATH=${CUDA_PATH} \
                    -D Kokkos_ENABLE_Pthread:BOOL=OFF"
  else
    export OMPI_CXX=`which gcc`
    unset CUDA_MANAGED_FORCE_DEVICE_ALLOC
    KOKKOS_SYMBOLS="-D Seacas_ENABLE_Kokkos:BOOL=ON \
                    -D Seacas_ENABLE_OpenMP:Bool=ON \
                    -D Kokkos_ENABLE_Pthread:BOOL=OFF"
  fi
else
  KOKKOS_SYMBOLS="-D Seacas_ENABLE_Kokkos:BOOL=OFF"
fi

rm -f CMakeCache.txt

###------------------------------------------------------------------------
cmake  \
-D CMAKE_CXX_FLAGS="-Wall -Wunused -pedantic" \
-D CMAKE_C_FLAGS="-Wall -Wunused -pedantic" \
-D CMAKE_MACOSX_RPATH:BOOL=ON \
-D CMAKE_INSTALL_RPATH:PATH=${INSTALL_PATH}/lib:${LD_LIBRARY_PATH} \
-D BUILD_SHARED_LIBS:BOOL=${SHARED} \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
-D Seacas_ENABLE_ALL_PACKAGES:BOOL=ON \
-D Seacas_ENABLE_ALL_OPTIONAL_PACKAGES:BOOL=ON \
-D Seacas_ENABLE_SECONDARY_TESTED_CODE:BOOL=ON \
-D Seacas_ENABLE_TESTS=ON \
-D CMAKE_INSTALL_PREFIX:PATH=${INSTALL_PATH} \
-D CMAKE_CXX_COMPILER:FILEPATH=${CXX} \
-D CMAKE_C_COMPILER:FILEPATH=${CC} \
-D CMAKE_Fortran_COMPILER:FILEPATH=${FC} \
-D Seacas_SKIP_FORTRANCINTERFACE_VERIFY_TEST:BOOL=ON \
-D Seacas_HIDE_DEPRECATED_CODE:BOOL=${OMIT_DEPRECATED_CODE} \
-D ENABLE_ExoNull:BOOL=YES \
\
-D TPL_ENABLE_Netcdf:BOOL=${HAVE_NETCDF} \
-D TPL_ENABLE_Matio:BOOL=${HAVE_MATIO} \
-D TPL_ENABLE_CGNS:BOOL=${HAVE_CGNS} \
-D TPL_ENABLE_MPI:BOOL=${MPI} \
-D TPL_ENABLE_Pamgen:BOOL=OFF \
-D TPL_ENABLE_Pthread:BOOL=${THREADSAFE} \
-D SEACASExodus_ENABLE_THREADSAFE:BOOL=${THREADSAFE} \
-D SEACASIoss_ENABLE_THREADSAFE:BOOL=${THREADSAFE} \
-D TPL_ENABLE_fmt:BOOL=ON \
-D TPL_ENABLE_X11:BOOL=${HAVE_X11} \
-D PythonInterp_FIND_VERSION:STRING=${PYTHON_VER} \
\
${KOKKOS_SYMBOLS} \
\
-D MPI_BIN_DIR:PATH=${MPI_BIN} \
-D NetCDF_ROOT:PATH=${NETCDF_PATH} \
-D HDF5_ROOT:PATH=${HDF5_PATH} \
-D HDF5_NO_SYSTEM_PATHS=ON \
-D CGNS_ROOT:PATH=${CGNS_PATH} \
-D PNetCDF_LIBRARIES=${SEMS_NETCDF_ROOT}/lib/libpnetcdf.a \
-D PNetCDF_INCLUDE_DIRS=${SEMS_NETCDF_ROOT}/include/ \
\
$EXTRA_ARGS \
..

OS=$(uname -s)
CC_VER=$(${CC} --version)
echo ""
echo "          OS: ${OS}"
echo "      ACCESS: ${ACCESS}"
echo "INSTALL_PATH: ${INSTALL_PATH}"
echo "  "
echo "         CC: ${CC}, ${CC_VER}"
echo "        CXX: ${CXX}"
echo "         FC: ${FC}"
echo "        MPI: ${MPI}"
echo "     SHARED: ${SHARED}"
echo " BUILD_TYPE: ${BUILD_TYPE}"
echo " THREADSAFE: ${THREADSAFE}"
echo " PYTHON_VER: ${PYTHON_VER}"
echo " "
echo "     NETCDF: ${HAVE_NETCDF}"
echo "      MATIO: ${HAVE_MATIO}"
echo "       CGNS: ${HAVE_CGNS}"
